<template>
  <!-- 组题列表 -->
  <div class="container">
    <!-- 搜索 -->
    <div class="searchFor">
      <el-form :inline="true" :model="formInline">
        <div class="leftSide">
          <p>关键字</p>
          <el-input
            v-model="formInline.user"
            placeholder="根据编号搜索"
            clearable
          ></el-input>
        </div>
        <div>
          <el-button size="small" @click="submit">清空</el-button>
          <el-button
            size="small"
            type="primary"
            @click="onSubmit(formInline.user)"
            >搜索</el-button
          >
        </div>
      </el-form>
    </div>
    <!-- 搜索 -->
    <!-- 记录 -->
    <div class="record">
      <i class="el-icon-info"></i>
      <span>共 {{ total }} 条记录</span>
    </div>
    <!-- 记录 -->
    <!-- table 表格数据区域 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column prop="id" label="编号" width="220"> </el-table-column>
      <el-table-column prop="questionType" label="题型" width="80">
        <template v-slot="{ row }">
          <span>{{ row.questionType ? '多选' : '简答' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="number" label="题目编号" width="220">
      </el-table-column>
      <el-table-column prop="addTime" label="录入时间" width="180">
      </el-table-column>
      <el-table-column prop="totalSeconds" label="答题时间(s)" min-width="80">
      </el-table-column>
      <el-table-column prop="accuracyRate" label="正确率(%)" min-width="80">
      </el-table-column>
      <el-table-column prop="userName" label="录入人" min-width="80">
      </el-table-column>
      <el-table-column prop="address" label="操作" width="80">
        <template #default="{ row }">
          <div class="actions">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="del(row.id)"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- table 表格数据区域 -->
    <!-- 分页组件区域 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryObj.page"
        :page-size="queryObj.pageSize"
        layout=" prev, pager, next, sizes, jumper"
        :page-sizes="[20, 30, 50]"
        background
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 分页组件区域 -->
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'

export default {
  data () {
    return {
      formInline: {
        user: ''
      },
      list: [],
      queryObj: {
        page: 1, // 控制当前页
        pageSize: 20 // 控制每一页有几条数据
      },
      dialogVisible: false, // 打开添加
      total: 0 // 总记录数
    }
  },

  created () {
    this.initTable()
  },
  methods: {
    // 获取table数据
    async initTable () {
      const { data } = await randoms(this.queryObj)
      // console.log(data)

      this.list = data.items
      this.total = data.counts
      console.log(this.list)
    },
    // 点击删除图标删除对应的数据
    async del (id) {
      console.log(id)
      // try {
      //   await removeRandoms(id)
      //   this.$message.success('删除成功')
      //   this.initTable()
      // } catch (error) {
      //   this.$message.error('删除失败')
      // }
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error',
        center: true
      })
        .then(async () => {
          console.log(id)
          await removeRandoms(id)
          this.$message({
            type: 'danger',
            message: '删除成功!'
          })
          this.initTable()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // pageSize 改变时会触发
    handleSizeChange (pagesize) {
      // console.log(`每页 ${pagesize} 条`)
      this.queryObj.pageSize = pagesize
      this.queryObj.current = 1
      this.initTable()
    },

    // current当前页改变时会触发
    handleCurrentChange (page) {
      // console.log(`当前页: ${page}`)
      // 改变参数对象中的当前页
      this.queryObj.current = page
      this.initTable()
    },
    // 清空搜索
    submit () {
      this.formInline.user = ''
      this.initTable()
    },
    // 搜索
    onSubmit (user) {
      console.log(this.list.id)
      console.log(user)
      this.list = this.list.filter(item => {
        console.log(item.username)
        return item.id === user
      })
    }
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 20px;
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  margin: 10px;
  .searchFor {
    width: 100%;
    display: flex;
    justify-content: space-between;
    height: 51px;
    .el-form {
      width: 100%;
      display: flex;
      justify-content: space-between;
      .leftSide {
        display: flex;
        width: 25%;
        ::v-deep p {
          width: 80px;
          height: 32px;
          margin-top: 10px;
          text-align: center;
          font-size: 14px;
          font-weight: 700;
        }
        ::v-deep .el-input {
          height: 32px;
          line-height: 32px;
          font-size: 13px;
          .el-input__inner {
            height: 100%;
            width: 100%;
          }
        }
      }
      .el-button {
        margin-left: 15px;
      }
    }
  }
  .record {
    width: 100%;
    background-color: #f4f4f5;
    color: #909399;
    padding: 8px 16px;
    span {
      padding: 0 8px;
      font-size: 13px;
      line-height: 18px;
    }
  }
  .block {
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
    ::v-deep .el-pagination__jump {
      margin-left: 0;
    }
  }
}
</style>
